package com.welike.shibo.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.welike.shibo.controller.BaseController;
import com.welike.shibo.eneity.*;
import com.welike.shibo.enums.ResultEnum;
import com.welike.shibo.service.*;
import com.welike.shibo.utils.ResultUtils;
import com.welike.shibo.vo.page.CommentMsgPageVo;
import com.welike.shibo.vo.page.FansPageVo;
import com.welike.shibo.vo.page.ZanMsgPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fangyu
 * @version v1.0.0
 * @since 2019-08-21 03:15
 */

@Slf4j
@Api(value = "消息相关接口")
@RestController
@RequestMapping("/api/msg")
public class ApiMsgController extends BaseController {

    @Autowired
    private FansService fansService;

    @Autowired
    private UserService userService;

    @Autowired
    private MsgService msgService;

    @Autowired
    private PushService pushService;

    // 极光推送
    @Autowired
    private JPushService jPushService;

    @Autowired
    private CommentsService commentsService;

    @ApiOperation(value = "查询收到评论的消息", notes = "查询视频收到新的评论消息")
    @GetMapping("/list/comment")
    public JsonResult getCommentMsgList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ) {
        map.clear();
        map.put("path", "/api/msg/list/comment");
        LoginUser loginUser = (LoginUser) getUser4Session();
        log.info("getCommentMsgList: loginUser ==> {}", loginUser);
        if (loginUser == null) {
            return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), map);
        }
        // 查询评论
//        List<CommentMsgPageVo> commentMsgPageVoList = commentsService.getUserCommentMsgPageList(page, pageSize, loginUser.getUid());
        List<CommentMsgPageVo> commentPageVoList = commentsService.getUserCommentMsgPageList(page, pageSize, loginUser.getUid());
        map.put("pageList", commentPageVoList);
        map.put("page", page);
        map.put("pageSize", pageSize);
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "查询收到粉丝关注的消息", notes = "查询用户收到新粉丝关注的消息")
    @GetMapping("/list/fans")
    public JsonResult getFansMsgList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ) {
        map.clear();
        map.put("path", "/api/msg/list/fans");
        LoginUser loginUser = (LoginUser) getUser4Session();
        log.info("getFansMsgList: loginUser ==> {}", loginUser);
        if (loginUser == null) {
            return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), map);
        }
        // 一、以自己为出发点(user_id = uid)，查询所有自己的fans_id
        List<FansPageVo> fansPageVoList = fansService.queryFansPage(page, pageSize, loginUser.getUid());
        map.put("pageList", fansPageVoList);
        map.put("page", page);
        map.put("pageSize", pageSize);
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "查询收到视频点赞的消息", notes = "查询用户收到新的视频点赞的消息")
    @GetMapping("/list/zan")
    public JsonResult getZanMsgList(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ) {
        map.clear();
        map.put("path", "/api/msg/list/zan");
        LoginUser loginUser = (LoginUser) getUser4Session();
        log.info("getZanMsgList: loginUser ==> {}", loginUser);
        if (loginUser == null) {
            return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), map);
        }
        List<ZanMsgPageVo> result = msgService.queryZanMsg(page, pageSize, loginUser.getUid());
        map.put("pageList", result);
        map.put("page", page);
        map.put("pageSize", pageSize);
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "推送开关列表", notes = "推送开关的列表")
    @GetMapping("/push/switch/list")
    public JsonResult pushSwitchList() {
        map.clear();
        map.put("path", "/api/msg/push/switch/list");
        LoginUser loginUser = (LoginUser) getUser4Session();
        log.info("pushSwitchList: loginUser ==> {}", loginUser);
        if (loginUser == null) {
            return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), map);
        }
        // 先查, 没有记录则插入
        Push push = pushService.queryByUid(loginUser.getUid());
        log.info("push ==> {}", push);
        // 如果为空则没有记录直接插入
        if (push == null) {
            push = new Push();
            push.setUid(loginUser.getUid());
            push.setZan(true);
            push.setFollow(true);
            push.setComment(true);
            pushService.save(push);
        }
        map.put("push_list", push);
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "推送开关", notes = "是否开启推送消息的开关")
    @PostMapping("/push/switch")
    public JsonResult pushSwitch(
            @RequestParam(value = "push_type", required = false, defaultValue = "1") int push_type
    ) {
        map.clear();
        map.put("path", "/api/msg/push/switch");
        LoginUser loginUser = (LoginUser) getUser4Session();
        log.info("pushSwitch: loginUser ==> {}", loginUser);
        if (loginUser == null) {
            return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), map);
        }
        Push push = pushService.queryByUid(loginUser.getUid());
        if (push_type == 1) {
            push.setZan(!push.getZan());
            map.put("switch", "点赞");
            map.put("state", push.getZan());
        } else if (push_type == 2) {
            push.setFollow(!push.getFollow());
            map.put("switch", "关注");
            map.put("state", push.getFollow());
        } else if (push_type == 3) {
            push.setComment(!push.getComment());
            map.put("switch", "评论和@");
            map.put("state", push.getComment());
        }
        pushService.updateByUid(push, loginUser.getUid());
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "推送测试", notes = "测试")
    @PostMapping("/push")
    public JsonResult pushAll(
            @RequestParam String title,
            @RequestParam String content
    ){
        map.clear();
        map.put("path", "/api/msg/push");
        PushBean pushBean = new PushBean();
        pushBean.setTitle(title);
        pushBean.setAlert(content);
        boolean flag = jPushService.pushIOS(pushBean);
//        boolean flag = jPushService.pushIOS(pushBean, "191e35f7e03a961f743");
        map.put("flag", flag);
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "推送历史", notes = "获取推送历史记录")
    @GetMapping("/push/history")
    public JsonResult pushHistory(
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize
    ){
        map.clear();
        map.put("path", "/api/msg/push/history");
        LoginUser loginUser = (LoginUser) getUser4Session();
        log.info("pushHistory: loginUser ==> {}", loginUser);
        if (loginUser == null) {
            return ResultUtils.error(ResultEnum.NOT_LOGIN.getCode(), ResultEnum.NOT_LOGIN.getMsg(), map);
        }
        IPage<Msg> result = msgService.queryAllPushMsg(page, pageSize, loginUser.getUid());
        map.put("pushHistory", result.getRecords());
        return ResultUtils.success(map);
    }

    @ApiOperation(value = "安卓极光推送测试", notes = "安卓极光推送测试")
    @PostMapping("/push_android")
    public JsonResult pushAndroid(
            @RequestParam String title,
            @RequestParam String content,
            @RequestParam String aliasId,
            @RequestParam Integer pushType
    ){
        map.clear();
        map.put("path", "/api/msg/push_android");
        PushBean pushBean = new PushBean();
        pushBean.setTitle(title);
        pushBean.setAlert(content);
        String uid = userService.queryUidByPhone(aliasId);
        userService.updateDeviceInfoByUid(aliasId, "android", uid);
        boolean flag = msgService.pushMsg(pushType, uid, title, content, null);

//        boolean flag = jPushService.pushIOS(pushBean, "191e35f7e03a961f743");
        map.put("flag", flag);
        map.put("explain", "返回");
        return ResultUtils.success(map);
    }
}